import java.util.Arrays;
import java.util.Comparator;

public class ComparatorTest {
    public static class Student {
        public int age;
        public int classroom;

        public Student (int age,int classroom) {
            this.age = age;
            this. classroom = classroom;
        }
    }

    public static class StudentComparator implements Comparator<Student> {
        @Override
        public int compare(Student e1, Student e2) {
            //返回负数表示e1的优先级更高
            //返回正数表示e2的优先级更高
            return e1.age - e2.age;
        }

    }

    public static void main(String[] args) {
        Student s1 = new Student(15,2);
        Student s2 = new Student(16,1);
        Student s3 = new Student(14,2);
        Student s4 = new Student(15,1);
        Student[] arr = {s1,s2,s3,s4};
        Arrays.sort(arr,new StudentComparator());
        for(Student e : arr) {
            System.out.println(e.classroom + " " + e.age);
        }

        System.out.println("==================");
        //按照班级从小到大排序，如果年龄一样按照年龄从小打到排序
        Arrays.sort(arr, (a,b) -> a.classroom != b.classroom ? (a.classroom - b.classroom) : (a.age - b.age) );
        for(Student e : arr) {
            System.out.println(e.classroom + " " + e.age);
        }
    }
}
